<template>
  <div class="cart-list" v-loading="loading">
    <div class="container">
      <!--  -->
      <div class="list-title" v-show="list.length != 0">
        <div class="all-checked">
          <div class="input" :class="{'ischecked': selectedAll}" @click="selectAll"></div>全选
        </div>
        <div class="product-name">商品名称</div>
        <div class="product-price">单价</div>
        <div class="product-number">数量</div>
        <div class="product-price-all">小计</div>
        <div class="product-remove">操作</div>
      </div>
      <!-- 商品 items -->
      <div class="product-list-item" v-for="item of list" :key="item.productId" v-show="list.length != 0">
        <div class="item-checked">
          <div class="input" :class="{'ischecked': item.productSelected}" @click="updateProductCount(item)"></div>
        </div>
        <div class="item-name">
          <img :src="item.productMainImage" />
          <span class="item-desc">{{ item.productName }}</span>
        </div>
        <div class="item-price">{{ item.productPrice }}元</div>
        <div class="item-number">
          <div class="item-number-add">
            <div class="number-add">
              <span class="iconfont" @click="updateProductCount(item, '-')"
                >&#xe620;</span
              >
              <!-- <span class="item-count">1</span> -->
              <!-- <input type="tel" value="1"> -->
              <input
                type="number"
                name=""
                id=""
                :value="item.quantity"
                min="1"
              />
              <span class="iconfont" @click="updateProductCount(item, '+')"
                >&#xe632;</span
              >
            </div>
          </div>
        </div>
        <div class="item-price-all">{{ item.productTotalPrice }}元</div>
        <div class="item-remove-btn">
          <span class="iconfont" @click="removeProduct(item.productId)"
            >&#xe66c;</span
          >
        </div>
      </div>
    </div>

    <div class="product-list-footer" v-show="list.length != 0">
      <div class="container flex-space-bet">
        <div class="left">
          <span class="toshoping">继续购物</span>
          <span class="product-count"
            >共 <span class="red">{{ list.length }}</span> 件商品，已选择
            <span class="red">{{ checkedCout }}</span> 件</span
          >
        </div>
        <div class="right">
          <span class="price-total"
            >合计：<span>{{ totalPrice }}</span
            >元</span
          >
          <button @click="goPayment">去结算</button>
        </div>
      </div>
    </div>
    <div class="nolist" v-show="list.length == 0">您还没有任何商品</div>
  </div>
</template>

<script>
import { Loading } from "element-ui";
export default {
  name: "CartList",
  data() {
    return {
      list: {},
      totalPrice: 0,
      checkedCout: 0,
      selectedAll: false,
      loading: true,
    };
  },
  methods: {
    goPayment() {
      this.$router.push("/cart/confirm");
    },
    cartsList() {
      this.axios.get("/carts")
      .then((res) => {
        console.log(this.axios)
        this.selectedAll = res.selectedAll;
        this.list = res.cartProductVoList;
        this.totalPrice = res.cartTotalPrice;
        this.loading = false;
        if (!this.list || this.list.length == 0) {
          this.loading = false;
          this.$confirm("空的购物车，前往商城添加商品", "提示", {
            confirmButtonText: "前往商城",
            cancelButtonText: "不想去",
          }).then(
            () => {
              this.$router.push("/index");
            },
            () => {
              return;
            }
          );
        }
      }, rej => {
        console.log(window.applicationCache.status)
        this.loading = false
        this.list.length = 0;
      });
    },
    removeProduct(id) {
      this.axios.delete(`/carts/${id}`).then((res) => {
        this.list = res.cartProductVoList;
        this.totalPrice = res.cartTotalPrice;
        this.$store.commit("saveProductsCount", res.cartTotalQuantity);
      });
    },
    updateProductCount(item, type) {
      let quantity = item.quantity,
        selected = item.productSelected;
      if (type === "-") {
        if (quantity <= 1) {
          // alert("商品至少保留一件");
          this.$message("商品至少保留一件");
          return;
        }
        --quantity;
      } else if (type === "+") {
        ++quantity;
      } else {
        selected = !selected;
        // item.productSelected = !item.productSelected
      }
      this.loading = true;
      this.axios
        .put(`/carts/${item.productId}`, {
          quantity,
          selected,
        })
        .then((res) => {
          this.list = res.cartProductVoList;
          this.totalPrice = res.cartTotalPrice;
          this.$store.commit("saveProductsCount", res.cartTotalQuantity);
          this.loading = false;
        });
    },
    selectAll() {
      let url = this.selectedAll ? "/carts/unSelectAll" : "/carts/selectAll";
      this.axios.put(url).then((res) => {
        // console.log(res)
        this.selectedAll = res.selectedAll;
        // let list = res.cartProductVoList;
        this.list = res.cartProductVoList;
        this.totalPrice = res.cartTotalPrice;
      });
    },
  },
  mounted() {
    this.cartsList();
  },
  watch: {
    list() {
      let count = 0;
      for (var item of this.list) {
        // console.log(item)
        if (item.productSelected === true) {
          count++;
        }
      }
      this.checkedCout = count;
      this.$store.commit("saveproducts", this.list);
    },
  },
};
</script>

<style lang="scss" scoped>
.cart-list {
  background: #eee;
  padding: 30px 0 0;
  .container {
    background: #fff;
    padding: 0 20px;
  }
  .list-title {
    display: flex;
    justify-content: space-between;
    line-height: 79px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #666666;
    .product-name {
      flex: 3;
    }
    .all-checked,
    .product-number,
    .product-price,
    .product-price-all,
    .product-remove {
      flex: 1;
    }
    .all-checked {
      input {
        margin-right: 5px;
        position: relative;
        top: 2px;
        border: none;
      }
    }
  }
  .product-list-item {
    display: flex;
    justify-content: space-between;
    height: 125px;
    line-height: 125px;
    font-weight: bold;
    font-size: 16px;
    border-top: 1px solid #eee;
    .item-checked,
    .item-price,
    .item-number,
    .item-price-all,
    .item-remove-btn {
      flex: 1;
    }
    
    .item-name {
      flex: 3;
      font-size: 17px;
      text-align: left;
      overflow: hidden;
      text-overflow: ellipsis; 
      img {
        width: 80px;
        vertical-align: middle;
        margin-right: 20px;
      }
    }
    .item-price-all {
      color: #ff6600;
    }
    .number-add {
      width: 150px;
      height: 40px;
      line-height: 40px;
      display: flex;
      justify-content: space-around;
      border: 1px solid #e5e5e5;
      position: relative;
      top: 45px;
      input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
        -webkit-appearance: none;
      }
      input {
        width: 30px;
        padding: 0;
        text-align: center;
        outline: none;
        border: none;
      }
      
    }
  }
  //  勾框样式
.item-checked,.all-checked{
      position: relative;
      .input{
        border-radius: 4px;
        border: 1px solid #999;
        width: 20px;
        height: 20px;
        display: inline-block;
        position: relative;
        top: 4px;
        left: -10px;
        img{
          width:100%;
          height: 100%;
          line-height: 20px;
        }
      } 
      .ischecked{
        background: #ff6600 url('/imgs/icon-gou.png') no-repeat center center;
        background-size: 15px;
        border: 1px solid #ff6600;
      }
    }
}
.product-list-footer {
  background: #eee;
  padding-bottom: 114px;
  .container {
    background: inherit;
    height: 50px;
    line-height: 50px;
    font-weight: bold;
    margin-top: 20px;
    .toshoping {
      font-size: 14px;
      color: #666666;
      margin-right: 40px;
    }
    .product-count {
      font-size: 14px;
      color: #666666;
    }
    .price-total {
      font-size: 14px;
      color: #ff6700;
      margin-right: 30px;
      span {
        font-size: 24px;
      }
    }
    button {
      width: 202px;
      height: 50px;
    }
  }
  
}
.nolist{
  padding: 100px 0;
  font-size: 30px;
  font-weight: bold;
  color: #ff6600;
}
</style>